package cn.com.assistant.service.impl;

import cn.com.assistant.entity.*;
import cn.com.assistant.mapper.*;
import cn.com.assistant.model.UserPOJO;
import cn.com.assistant.service.TableService;
import cn.com.assistant.service.UserService;
import cn.com.assistant.utils.ExcelDataVo;
import cn.com.assistant.utils.PackExcelSheetsDataUtil;
import cn.com.assistant.utils.ResponseMessage;
import cn.com.assistant.utils.TokenUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TableServiceImpl extends ServiceImpl<TableMapper, TableEntity> implements TableService {

    @Autowired
    TableMapper tableMapper;

    @Autowired
    SchemaMapper schemaMapper;

    @Autowired
    DataMapper dataMapper;

    @Autowired
    SynonymMapper synonymMapper;

    @Override
    public ResponseMessage findAllTable() {
        List<Map<String, Object>> allTable = tableMapper.findAllTable();
        return ResponseMessage.defaultResponse().setData(allTable);
    }

    @Override
    public void exportExcel(String id, HttpServletResponse response) throws Exception {

        // 查询主体信息
        TableEntity tableEntity = this.baseMapper.selectById(id);

        // 查询属性信息
        List<SchemaEntity> schemaEntityList = schemaMapper.findByPId(id);

        // 查询实体数据信息
        List<DataEntity> dataEntityList = dataMapper.findByPId(id);

        // 查询同义词
        List<SynonymEntity> synonymEntityList = synonymMapper.findByPId(id);


        // 定义主体信息
        List<Map<String, Object>> list1 = Lists.newArrayList();
        Map<String, Object> map1 = Maps.newHashMap();
        map1.put("chineseName", tableEntity.getChineseName());
        map1.put("englishName", tableEntity.getEnglishName());
        list1.add(map1);

        // 定义属性信息
        List<Map<String, Object>> list2 = Lists.newArrayList();
        for (int i = 0; i < schemaEntityList.size(); i++) {
            SchemaEntity schemaEntity = schemaEntityList.get(i);
            Map<String, Object> map2 = Maps.newHashMap();
            map2.put("attribute", schemaEntity.getAttribute());
            map2.put("attributeType", schemaEntity.getAttributeType());
            map2.put("attributeName", schemaEntity.getAttributeName());
            map2.put("customReply", schemaEntity.getCustomReply());
            list2.add(map2);
        }

        // 定义实体数据信息
        List<Map<String, Object>> list3 = Lists.newArrayList();
        for (int i = 0; i < dataEntityList.size(); i++) {
            DataEntity dataEntity = dataEntityList.get(i);
            Map<String, Object> map3 = Maps.newHashMap();
            map3.put("question", dataEntity.getQuestion());
            map3.put("type", dataEntity.getType());
            map3.put("solutions", dataEntity.getSolutions());
            list3.add(map3);
        }
        // 定义实体数据信息
        for (int i = 0; i < dataEntityList.size(); i++) {
            DataEntity dataEntity = dataEntityList.get(i);
            Map<String, Object> map3 = Maps.newHashMap();
            map3.put("question", dataEntity.getFaultName());
            map3.put("type", dataEntity.getType());
            map3.put("solutions", dataEntity.getSolutions());
            list3.add(map3);
        }

        // 定义同义词信息
        List<Map<String, Object>> list4 = Lists.newArrayList();
        for (int i = 0; i < synonymEntityList.size(); i++) {
            SynonymEntity synonymEntity = synonymEntityList.get(i);
            Map<String, Object> map4 = Maps.newHashMap();
            map4.put("keyword", synonymEntity.getKeyword());
            map4.put("normalWord", synonymEntity.getNormalWord());
            list4.add(map4);
        }


        // 封装需要导出的数据
        List<ExcelDataVo> excelDataVoList = Lists.newArrayList();

        // 封装主题信息
        ExcelDataVo excelDataSheetOne = new ExcelDataVo();
        excelDataSheetOne.setSheetName("table");
//        excelDataSheetOne.setSheetTopColHeaderTitle("学生信息");
        excelDataSheetOne.setSheetTopColHeaderName(new String[] {"表格中文名","表格英文名"});
        excelDataSheetOne.setSheetTopColHeaderAttribute(new String[] {"chineseName","englishName"});
        excelDataSheetOne.setSheetDataList(list1);
        excelDataVoList.add(excelDataSheetOne);

        // 封装属性信息
        ExcelDataVo excelDataSheetTwo = new ExcelDataVo();
        excelDataSheetTwo.setSheetName("schema");
//        excelDataSheetTwo.setSheetTopColHeaderTitle("学校信息");
        excelDataSheetTwo.setSheetTopColHeaderName(new String[] {"属性名英文","属性类型","属性名（多个用|隔开）","自定义回复话术"});
        excelDataSheetTwo.setSheetTopColHeaderAttribute(new String[] {"attribute","attributeType","attributeName","customReply"});
        excelDataSheetTwo.setSheetDataList(list2);
        excelDataVoList.add(excelDataSheetTwo);

        // 封装实体数据信息
        ExcelDataVo excelDataSheetThree = new ExcelDataVo();
        excelDataSheetThree.setSheetName("data");
//        excelDataSheetThree.setSheetTopColHeaderTitle("其他信息");
        excelDataSheetThree.setSheetTopColHeaderName(new String[] {"故障表现","故障类型","解决措施"});
        excelDataSheetThree.setSheetTopColHeaderAttribute(new String[] {"question","type","solutions"});
        excelDataSheetThree.setSheetDataList(list3);
        excelDataVoList.add(excelDataSheetThree);

        // 封装同义词信息
        ExcelDataVo excelDataSheetFour = new ExcelDataVo();
        excelDataSheetFour.setSheetName("synonym");
//        excelDataSheetFour.setSheetTopColHeaderTitle("其他信息");
        excelDataSheetFour.setSheetTopColHeaderName(new String[] {"关键词","归一化词"});
        excelDataSheetFour.setSheetTopColHeaderAttribute(new String[] {"keyword","normalWord"});
        excelDataSheetFour.setSheetDataList(list4);
        excelDataVoList.add(excelDataSheetFour);

        // 导出数据
        try {
            new PackExcelSheetsDataUtil().packExcelSheetsData(response, "故障问答", excelDataVoList);
        } catch (Exception e) {
            throw new Exception("导出异常!");
        }
    }
}